package com.hm.controller;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.lang.ProcessBuilder.Redirect;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.tomcat.util.http.fileupload.ByteArrayOutputStream;
import org.hibernate.validator.internal.util.privilegedactions.GetAnnotationParameter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hm.mapper.BookMapper1225;
import com.hm.model.Book1225;
import com.hm.model.Employee;

import redis.clients.jedis.Jedis;

@Controller
@RequestMapping("book")
public class BookController1225 {

	@GetMapping("sess")
	public @ResponseBody Object testSession(HttpSession hs) {
		Employee user = new Employee();
		user.setname("桂花");
		user.setuAge(25);
		hs.setAttribute("user", user);
		return user;
	}
	
	
	
	@Resource
	private Jedis jedis;
	
	@GetMapping("hehe")
	public @ResponseBody Object hehe() throws ClassNotFoundException {
		/*Employee employee = new Employee();
		employee.setname("张春华");
		employee.setuAge(19);
		
		
		try (ByteArrayOutputStream out = new ByteArrayOutputStream();
				ObjectOutputStream oout = new ObjectOutputStream(out);){
			oout.writeObject(employee);
			jedis.set("emp".getBytes(), out.toByteArray());
		} catch (IOException e) {
			e.printStackTrace();
		}*/
		
		byte[] bs = jedis.get("emp".getBytes());
		
		try (ObjectInputStream ois= new ObjectInputStream(new ByteArrayInputStream(bs));
				){
			Object readObject = ois.readObject();
			return readObject;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "sussess";
	}
	
	@Resource
	private BookMapper1225 bookMapper1225;
	
	@GetMapping("ByBname")
	public @ResponseBody Object findByBname(String bookName) {
		 	
		  return bookMapper1225.findByBname(bookName);
	}
	
	@GetMapping("findAll")
	public List<Book1225> findAll(@Valid Map<String, Object> map) {
		List<Book1225> books = bookMapper1225.findAll();
		map.put("books", books);
		return books;
	}

	@GetMapping("delete")
	public  ModelAndView delete(String bookName){
		   bookMapper1225.delete(bookName);
		   return new ModelAndView("redirect:findAll");
	}
	
	@GetMapping("insert")
	public  ModelAndView insert(String bookName,String auth,Double price,String type,Date date,String pAddress,String publish){
		bookMapper1225.insert(bookName, auth, price, type, date, pAddress, publish);
		return new ModelAndView("redirect:findAll");
	}
	
	
}
